package com.github.postsanf.yinian.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.adapter.ZoomBigAdapter;
import com.github.postsanf.yinian.constants.SelectedImg;
import com.github.postsanf.yinian.view.MultiTouchViewPager;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

public class ImageLocalZoomBigActivity extends BaseActivity implements View.OnClickListener, ZoomBigAdapter.Callback {
    private MultiTouchViewPager vp_zoombig_image;                       // 整个切换视图
    private ImageView iv_zoombig_select;
    private LinearLayout zoombig_switch;
    private ImageView zoombig_switch_iv;
    private TextView zoombig_switch_tx;
    private LinearLayout ll_select_tip;
    private TextView tv_num;
    private RelativeLayout rl_zoombig_top;
    private RelativeLayout rl_zoombig_buttom;
    private ZoomBigAdapter mAdapter;                               // 图片适配器
    private List<String> picUrlsList = new ArrayList<>();       // 图片数据
    private int position = 0;
    private long fristtime = 0;
    private TextView tv_next;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        fristtime = System.currentTimeMillis();
        setContentView(R.layout.yn_imagelocal_zoombig);
        initData();
        initView();
        initViewPager();
    }

    private void initData() {
        boolean typeyun = getIntent().getBooleanExtra("typeyun", false);
        boolean typelocalAll = getIntent().getBooleanExtra("typelocalAll", false);
        boolean typeSelted = getIntent().getBooleanExtra("typeSelted", false);
        if (typeyun) {
            picUrlsList = getIntent().getStringArrayListExtra("picUrlsList");
        } else if (typelocalAll) {
            picUrlsList = ImageLocalSelectActivity.mImgs;
        } else if (typeSelted) {
            picUrlsList = getIntent().getStringArrayListExtra("picUrlsList");
        }
        position = getIntent().getIntExtra("position", 0);
    }

    private void initView() {
        findViewById(R.id.iv_back).setOnClickListener(this);
        tv_next = (TextView) findViewById(R.id.tv_next);
        tv_next.setOnClickListener(this);
        if (!ImageLocalSelectActivity.mainImage) {
            tv_next.setVisibility(View.GONE);
        } else {
            tv_next.setVisibility(View.VISIBLE);
        }
        rl_zoombig_top = (RelativeLayout) findViewById(R.id.rl_zoombig_top);
        rl_zoombig_buttom = (RelativeLayout) findViewById(R.id.rl_zoombig_buttom);
        iv_zoombig_select = (ImageView) findViewById(R.id.iv_zoombig_select);
        vp_zoombig_image = (MultiTouchViewPager) findViewById(R.id.vp_zoombig_image);
        zoombig_switch = (LinearLayout) findViewById(R.id.zoombig_switch);
        zoombig_switch_iv = (ImageView) findViewById(R.id.zoombig_switch_iv);
        zoombig_switch_tx = (TextView) findViewById(R.id.zoombig_switch_tx);
        ll_select_tip = (LinearLayout) findViewById(R.id.ll_select_tip);
        tv_num = (TextView) findViewById(R.id.tv_num);
        iv_zoombig_select.setOnClickListener(this);
        if (ImageLocalSelectActivity.IsSmall) {
            zoombig_switch_iv.setImageResource(R.drawable.zoombig_switch_zoonin);
            zoombig_switch_tx.setText("缩略图");
        } else {
            zoombig_switch_iv.setImageResource(R.drawable.zoombig_switch_normal);
            zoombig_switch_tx.setText("原图");
        }
        zoombig_switch.setOnClickListener(this);
        updateNextTip();
    }

    private void updateNextTip() {
        if (SelectedImg.mSelectedImage.size() > 0) {
            ll_select_tip.setVisibility(View.VISIBLE);
            tv_num.setText("" + SelectedImg.mSelectedImage.size());
        } else {
            ll_select_tip.setVisibility(View.GONE);
        }
    }

    boolean picselect = false;
    boolean picswitch = false;
    boolean tOPShow = false;

    @Override
    public void onPhotoTap(View view, float x, float y) {
        long pathftime = System.currentTimeMillis() - fristtime;
        Log.i("xjs", "pathftime==>" + pathftime);
        if (pathftime < 800) {
            finish();
        }
        if (!tOPShow) {
            rl_zoombig_top.setVisibility(View.GONE);
            rl_zoombig_buttom.setVisibility(View.GONE);
            tOPShow = true;
        } else {
            rl_zoombig_top.setVisibility(View.VISIBLE);
            rl_zoombig_buttom.setVisibility(View.VISIBLE);
            tOPShow = false;
        }
    }

    private void initViewPager() {
        final int size = picUrlsList.size();
        mAdapter = new ZoomBigAdapter(this, picUrlsList, this);
        vp_zoombig_image.setAdapter(mAdapter);
        if (position == 0) {
            if (SelectedImg.mSelectedImage.contains(picUrlsList.get(0))) {
                picselect = true;
                iv_zoombig_select.setImageResource(R.drawable.iv_zoombig_select);
            } else {
                picselect = false;
                iv_zoombig_select.setImageResource(R.drawable.iv_zoombig_unselect);
            }
        }
        vp_zoombig_image.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int arg0) {
//                int index = arg0 % size;
                String thisurl = picUrlsList.get(arg0);
//                iv_zoombig_select.setTag(thisurl);
                if (SelectedImg.mSelectedImage.contains(thisurl)) {
                    picselect = true;
                    iv_zoombig_select.setImageResource(R.drawable.iv_zoombig_select);
                } else {
                    picselect = false;
                    iv_zoombig_select.setImageResource(R.drawable.iv_zoombig_unselect);
                }
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
        vp_zoombig_image.setCurrentItem(position);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_next:
                if (SelectedImg.mSelectedImage.size() == 0) {
                    Toast.makeText(this, "至少选择一张图片", Toast.LENGTH_SHORT).show();
                    return;
                }
                intent2Activity(ImageLocalEditActivity.class);
                finish();
                break;
            case R.id.iv_zoombig_select:
                if (picselect) {
                    picselect = false;
                    iv_zoombig_select.setImageResource(R.drawable.iv_zoombig_unselect);
//                    String url = (String) iv_zoombig_select.getTag();
                    String url = picUrlsList.get(vp_zoombig_image.getCurrentItem());
                    if (SelectedImg.mSelectedImage.contains(url)) {
                        SelectedImg.mSelectedImage.remove(url);
                    }
                } else {
                    picselect = true;
                    iv_zoombig_select.setImageResource(R.drawable.iv_zoombig_select);
//                    String url = (String) iv_zoombig_select.getTag();
                    String url = picUrlsList.get(vp_zoombig_image.getCurrentItem());
                    SelectedImg.mSelectedImage.add(url);
                }
                updateNextTip();
                sendBroadcast(new Intent("updatetopgridview"));
                sendBroadcast(new Intent("updatebuttomgridview"));
                break;
            case R.id.zoombig_switch:
                if (ImageLocalSelectActivity.IsSmall) {
                    ImageLocalSelectActivity.IsSmall = false;
                    zoombig_switch_iv.setImageResource(R.drawable.zoombig_switch_normal);
                    zoombig_switch_tx.setText("原图");
                } else {
                    ImageLocalSelectActivity.IsSmall = true;
                    zoombig_switch_iv.setImageResource(R.drawable.zoombig_switch_zoonin);
                    zoombig_switch_tx.setText("缩略图");
                }
                break;
            default:
                break;
        }
    }

    // 友盟统计
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }
}
